package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"yk/common/proto/user"
)

type Handle func(ctx context.Context, client user.UserClient) (interface{}, error)

func UserGrpc(ctx context.Context, handle Handle) (interface{}, error) {
	conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	client := user.NewUserClient(conn)
	res, err := handle(ctx, client)
	if err != nil {
		return nil, err
	}
	defer conn.Close()
	return res, nil
}

func Reg(ctx context.Context, in *user.RegRequest) (*user.RegResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		reg, err := client.Reg(ctx, in)
		if err != nil {
			return nil, err
		}
		return reg, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.RegResponse), nil
}

func Update(ctx context.Context, in *user.UpdateRequest) (*user.UpdateResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		update, err := client.Update(ctx, in)
		if err != nil {
			return nil, err
		}
		return update, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.UpdateResponse), nil
}

func UserXy(ctx context.Context, in *user.UserXyRequest) (*user.UserXyResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		xy, err := client.UserXy(ctx, in)
		if err != nil {
			return nil, err
		}
		return xy, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.UserXyResponse), nil
}

func OrderCarList(ctx context.Context, in *user.OrderCarListRequest) (*user.OrderCarListResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		list, err := client.OrderCarList(ctx, in)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.OrderCarListResponse), nil
}

func TakeCreate(ctx context.Context, in *user.TakeCreateRequest) (*user.TakeCreateResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		create, err := client.TakeCreate(ctx, in)
		if err != nil {
			return nil, err
		}
		return create, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.TakeCreateResponse), nil
}

func Login(ctx context.Context, in *user.LoginRequest) (*user.LoginResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.Login(ctx, in)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.LoginResponse), nil
}

func EvaCreate(ctx context.Context, in *user.EvaCreateRequest) (*user.EvaCreateResponse, error) {
	userGrpc, err := UserGrpc(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		create, err := client.EvaCreate(ctx, in)
		if err != nil {
			return nil, err
		}
		return create, nil
	})
	if err != nil {
		return nil, err
	}
	return userGrpc.(*user.EvaCreateResponse), nil
}
